package com.wchc.admin.user.infrastructure.persistence.repository;
import com.wchc.admin.user.domain.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;


import java.util.Optional;
import java.util.concurrent.CompletableFuture;

@Repository
@RequiredArgsConstructor
public class UserRepositoryImpl implements UserRepository {
    
    private final UserMapper userMapper;
    
    @Override
    public CompletableFuture<Optional<User>> findById(Long id) {
        return CompletableFuture.supplyAsync(() -> userMapper.findById(id));
    }
    
    @Override
    public CompletableFuture<Optional<User>> findByUsername(String username) {
        return CompletableFuture.supplyAsync(() -> userMapper.findByUsername(username));
    }
    
    @Override
    public CompletableFuture<Long> save(User user) {
        return CompletableFuture.supplyAsync(() -> {
            if (user.getId() == null) {
                userMapper.insert(user);
            } else {
                userMapper.update(user);
            }
            return user.getId();
        });
    }
    
    @Override
    public CompletableFuture<Void> delete(Long id) {
        return CompletableFuture.runAsync(() -> userMapper.delete(id));
    }
}